package com.his.his_system.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.his.his_system.common.util.ResultAdvice;
import com.his.his_system.common.vo.PageHelp;
import com.his.his_system.domain.DzmHisDepartment;
import com.his.his_system.domain.DzmHisMember;
import com.his.his_system.service.DzmHisDepartmentService;
import com.his.his_system.service.DzmHisMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/his/sys/dept/")
@RestController
@ResponseBody
public class DepartmentController {
    @Autowired
    private DzmHisDepartmentService deptService;

    @Autowired
    private DzmHisMemberService memberService;

    /**
     * 查询全部
     * @param param
     * @return
     */
    @GetMapping("/list")
    public ResultAdvice list(@RequestParam(required = false)PageHelp param) {
        if (param != null) {
            PageHelper.startPage(param.getCurrentPage(),param.getPageSize());
        }
        List<DzmHisDepartment> list = deptService.list();
        return ResultAdvice.success(list,"全部科室");
    }

    /**
     * 根据名称查询
     * @param name
     * @return
     */
    @GetMapping("/getByName")
    public ResultAdvice list(String name) {
        DzmHisDepartment one =
                deptService.getOne(new QueryWrapper<DzmHisDepartment>().eq("department_name", name));
        return ResultAdvice.success(one,"全部科室");
    }


    /**
     * 根据科室名称模糊查询
     * @param
     * @return
     */
    @GetMapping("/like")
    public ResultAdvice like(String keyWord) {
        QueryWrapper<DzmHisDepartment> qr =
                new QueryWrapper<DzmHisDepartment>().like("department_name",keyWord);
        return ResultAdvice.success(deptService.list(qr),"全部科室");
    }

    /**
     * 修改
     * @param department 部门
     * @return
     */
    @PostMapping("/modify")
    public ResultAdvice modify(@RequestBody DzmHisDepartment department) {
        return ResultAdvice.success(deptService.updateById(department),"全部科室");
    }
    /**
     * 添加
     * @param department 部门
     * @return
     */
    @PostMapping("/add")
    public ResultAdvice add(@RequestBody DzmHisDepartment department) {
        department.setDepartmentNumber(System.currentTimeMillis()+"");
        department.setHid(1);
        deptService.save(department);
        return ResultAdvice.success("添加成功");
    }

    /**
     * 删除
     * @param did 科室ID
     * @return
     */
    @PostMapping("/del/{did}")
    public ResultAdvice modify(@PathVariable("did") Integer did) {
        QueryWrapper<DzmHisMember> qr =
                new QueryWrapper<DzmHisMember>()
                        .eq("department_id",did).select("department_id");
        List<DzmHisMember> one = memberService.list(qr);

        if (one.size()!=0){
            return ResultAdvice.fail("该科室有医生，不能删除","319");
        }
        return ResultAdvice.success(deptService.removeById(did),"全部科室");
    }


}
